package pl.enigmatic.reflection;

import pl.enigmatic.util.Arrays;
import pl.enigmatic.util.Strings;


public class PropertyPath {

	private final String[] path;

	public PropertyPath(String[] path) {
		this.path = path;
	}

	public PropertyPath(final String qualifiedPath) {
		this(qualifiedPath.split("\\."));
	}

	public int getDepth() {
		return path.length;
	}

	public String get(final int depth) {
		return path[depth];
	}

	public boolean isEmpty() {
		return getDepth() == 0;
	}

	public boolean isNested() {
		return getDepth() > 1;
	}

	public boolean isSimple() {
		return !isNested();
	}

	public String first() {
		return path[0];
	}

	public String last() {
		return path[getDepth() - 1];
	}

	public PropertyPath prefixed() {
		return new PropertyPath(Arrays.changeSize(path, getDepth() - 1, false, 0));
	}

	public PropertyPath suffixed() {
		return new PropertyPath(Arrays.changeSize(path, getDepth() - 1, true, 0));
	}

	@Override
	public String toString() {
		return Strings.concatenate(".", path);
	}
}
